package com.biz.hunter.dao;

import com.biz.hunter.db.entity.TempRecurit;
import com.biz.hunter.dbutil.BaseDAO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Repository
public class TempRecuritDAO {

	private static final String SELECT_BY_ID = "select * from tb_temp_recurit where jobId = ?";
	public TempRecurit selectById(int jobId) {
		return BaseDAO.queryForObject(TempRecurit.class,SELECT_BY_ID, jobId);
	}

	public List<TempRecurit> selectList(String selectParams, String companyDomain ,String city, int offset, String companyBusiness, int pageSize) {
		if (StringUtils.isEmpty(companyBusiness)) {
			String sql = "select * from tb_temp_recurit where active = 1 order by companyBusiness,sort asc,updateTime desc limit ?, ?";
			if (StringUtils.isNotBlank(city) && StringUtils.isNotBlank(selectParams) && StringUtils.isNotBlank(companyDomain)) {
				//都有
				sql = "select * from tb_temp_recurit where jobPlace like '%"+city+"%' and companyDomain = '"+companyDomain
						+"' and  companyName like '%"+selectParams+"%' order by companyBusiness,sort asc,updateTime desc limit ?,?";
				List<TempRecurit> tempRecurits = BaseDAO.queryForList(TempRecurit.class, sql, offset, pageSize);
				if (tempRecurits == null || tempRecurits.size() == 0) {
					sql = "select * from tb_temp_recurit where jobPlace like '%"+city+"%' and companyDomain = '"+companyDomain
							+"' and  jobName like '%"+selectParams+"%' order by companyBusiness,sort asc,updateTime desc limit ?,?";
				} else {
					return tempRecurits;
				}
			}  else if (!StringUtils.isNotBlank(city) && !StringUtils.isNotBlank(selectParams) && StringUtils.isNotBlank(companyDomain)) {
				//领域
				sql = "select * from tb_temp_recurit where  companyDomain = '"+companyDomain
						+"' order by companyBusiness,sort asc,updateTime desc limit ?,?";
			} else if (!StringUtils.isNotBlank(city) && StringUtils.isNotBlank(selectParams) && !StringUtils.isNotBlank(companyDomain)) {
				//自定义
				sql = "select * from tb_temp_recurit where companyName like '%"+selectParams+"%' order by companyBusiness,sort asc,updateTime desc limit ?,?";
				List<TempRecurit> tempRecurits = BaseDAO.queryForList(TempRecurit.class, sql, offset, pageSize);
				if (tempRecurits == null || tempRecurits.size() == 0) {
					sql = "select * from tb_temp_recurit where jobName like '%"+selectParams+"%' order by companyBusiness,sort asc,updateTime desc limit ?,?";
				} else {
					return tempRecurits;
				}
			} else if (StringUtils.isNotBlank(city) && !StringUtils.isNotBlank(selectParams) && !StringUtils.isNotBlank(companyDomain)) {
				//城市
				sql = "select * from tb_temp_recurit where jobPlace like '%"+city+"%' order by companyBusiness,sort asc,updateTime desc limit ?,?";
			} else if (StringUtils.isNotBlank(city) && !StringUtils.isNotBlank(selectParams) && StringUtils.isNotBlank(companyDomain)) {
				//领域，城市
				sql = "select * from tb_temp_recurit where jobPlace like '%"+city+"%' and companyDomain = '"+companyDomain
						+"' order by companyBusiness,sort asc,updateTime desc limit ?,?";
			} else if (!StringUtils.isNotBlank(city) && StringUtils.isNotBlank(selectParams) && StringUtils.isNotBlank(companyDomain)) {
				//自定义，领域
				sql = "select * from tb_temp_recurit where companyDomain = '"+companyDomain
						+"' and  companyName like '%"+selectParams+"%' order by companyBusiness,sort asc,updateTime desc limit ?,?";
				List<TempRecurit> tempRecurits = BaseDAO.queryForList(TempRecurit.class, sql, offset, pageSize);
				if (tempRecurits == null || tempRecurits.size() == 0) {
					sql = "select * from tb_temp_recurit where companyDomain = '"+companyDomain
							+"' and  jobName like '%"+selectParams+"%' order by companyBusiness,sort asc,updateTime desc limit ?,?";
				} else {
					return tempRecurits;
				}
			} else if (StringUtils.isNotBlank(city) && StringUtils.isNotBlank(selectParams) && !StringUtils.isNotBlank(companyDomain)) {
				//自定义，城市
				sql = "select * from tb_temp_recurit where jobPlace like '%"+city+"%' and companyName like '%"+selectParams+"%' order by companyBusiness,sort asc,updateTime desc limit ?,?";
				List<TempRecurit> tempRecurits = BaseDAO.queryForList(TempRecurit.class, sql, offset, pageSize);
				if (tempRecurits == null || tempRecurits.size() == 0) {
					sql = "select * from tb_temp_recurit where jobPlace like '%"+city+"%' and  jobName like '%"+selectParams+"%' order by companyBusiness,sort asc,updateTime desc limit ?,?";
				} else {
					return tempRecurits;
				}
			}
			System.out.println("查询招聘职位SQL：" + sql);
			return BaseDAO.queryForList(TempRecurit.class, sql, offset, pageSize);
		} else {
			String sql = "select * from tb_temp_recurit where companyBusiness = "+ companyBusiness +" and active = 1 order by companyBusiness,sort asc,updateTime desc limit ?, ?";
			if (StringUtils.isNotEmpty(city)) {
				sql = "select * from tb_temp_recurit where companyBusiness = "+ companyBusiness +" and jobPlace = '"+ city +"' and active = 1 order by companyBusiness,sort asc,updateTime desc limit ?, ?";
			}
			if (StringUtils.isNotEmpty(selectParams)) {
				sql = "select * from tb_temp_recurit where companyBusiness = "+ companyBusiness +" and companyName = '"+ selectParams +"' and active = 1 order by companyBusiness,sort asc,updateTime desc limit ?, ?";
				List<TempRecurit> tempRecurits = BaseDAO.queryForList(TempRecurit.class, sql, offset, pageSize);
				if (tempRecurits.isEmpty()) {
					sql = "select * from tb_temp_recurit where companyBusiness = "+ companyBusiness +" and jobName = '"+ selectParams +"' and active = 1 order by companyBusiness,sort asc,updateTime desc limit ?, ?";
				} else {
					return tempRecurits;
				}
			}

			System.out.println("查询招聘职位SQL：" + sql);
			return BaseDAO.queryForList(TempRecurit.class, sql, offset, pageSize);
		}

	}

}
